﻿using Kugar.Core.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kugar.Core.ExtMethod;
using Kugar.Core.Log;
using Kugar.Tools.IM.Tencent.ServerSDK;
using Newtonsoft.Json.Linq;

namespace Kugar.Tools.IM.Tencent.ServerNotify
{
    public class TencentIMNotifyController:Controller
    {
        public ActionResult Notify()
        {
            var dt = DateTime.Now;
            
            
            var sdkID = Request.GetString("SdkAppid");
            var command = Request.GetString("CallbackCommand");

            Request.InputStream.Position = 0;

            var jsonStr = Request.InputStream.ReadToEnd(Request.ContentEncoding);

            Request.InputStream.Position = 0;

            LoggerManager.Default.Debug($"接收到数据:Url={Request.Url};Data={jsonStr}");

            var json = JObject.Parse(jsonStr);

            try
            {
                TencentIMApi.ProcessNotifyMessage(dt,sdkID, command, json);

                return this.NewtonJson(new JObject()
                                       {
                                           ["ActionStatus"] = "OK",
                                           ["ErrorInfo"] = "",
                                           ["ErrorCode"] = 0
                                       });
            }
            catch (Exception e)
            {
                return this.NewtonJson(new JObject()
                                       {
                                           ["ActionStatus"] = "FAIL",
                                           ["ErrorInfo"] = e.Message,
                                           ["ErrorCode"] =1
                                       });
            }
            
            
            
        }
    }
}
